<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Container\Container;
class AdminController extends Container{

    /**
     * 容器 实例映射表
     * @var array
     */
    protected $aliases = [
        'Member'    =>  '\Admin\Model\MemberModel',
//        'Menu'    =>  '\Admin\Model\MenuModel',
        'Tree'      =>  '\Common\Model\TreeModel',
        'Wechat'    =>  '\Common\Tool\Wechat',
    ];

    /**
     * 前台静态提醒
     * @var array
     */
    public $alert = ['status'=>0,'msg'=>'','data'=>null];

    /**
     * 免登陆白名单
     */
    protected $whiteList = [
        'Wechat'    =>  [
            'api',
        ],
        'Public',
        'File'
    ];

    /**
     * 后台控制器初始化操作
     */
    public function _initialize(){

        parent::_initialize();

        if ( $_REQUEST['session_id'] ) {
            header('debug: have session_id='.$_REQUEST['session_id']);
            session_id($_REQUEST['session_id']);
            session_start();
        } else {
            header('debug: have not session_id');
        }

        // 获取当前用户ID
        if(defined('UID')) return ;
        define('UID',is_login_admin());
        if( !UID && !check_white_list($this->whiteList)){// 还没登录 跳转到登录页面
            $this->redirect('Public/login');
        }

        // 读取数据库中的配置,先判断是否设置了缓存
        if ( C('CACHE_CONFIG') ){
            // 缓存
            $config	= S('DB_CONFIG');
        } else {
            // 不缓存
            $config = null;
        }

        if(!$config){
            $Config     =   new \Common\Tool\Config();
            $config =   $Config::lists();//解析的配置数据
            S('DB_CONFIG',$config);
        }
        C($config); //添加配置

        // 读取当前微信公众号设置信息
//        dump($_SERVER['HTTP_HOST']);
    }

    public function alert($status = 0, $msg = '', $data = null){
        $this->alert['status'] = $status?:$this->alert['status'];
        $this->alert['msg'] = $msg?:$this->alert['msg'];
        $this->alert['data'] = $data?:$this->alert['data'];
        $this->assign('alert',$this->alert);
    }
}
